package cn.itcast.producer;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Produer {
    public static void main(String[] args) throws IOException, TimeoutException {
        try {
            //rabbitmq连接
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("192.168.200.128");
            factory.setPort(5672);
            factory.setUsername("guest");
            factory.setPassword("guest");
            factory.setVirtualHost("/");

            //建立长连接
            Connection connection = factory.newConnection();

            //建立通道
            Channel channel = connection.createChannel();

            //声明队列
            /* 参数1：队列名称
             * 参数2：是否定义持久化队列
             * 参数3：是否独占本次连接
             * 参数4：是否在不使用的时候自动删除队列
             * 参数5：队列其它参数
             */
            channel.queueDeclare("xxx",true,false,false,null);

            //发消息
            //String exchange,  交换机名称
            //String routingKey  路由键
            //BasicProperties  参数
            //byte[] body   消息
            String msg = "hello";
            channel.basicPublish("","xxx",null,msg.getBytes());

            //关流
            channel.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
